package moodish.client;

import java.awt.Color;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

/**
 * This class represents the Talk panel where the mood updates are shown.
 * 
 * @author es1-2013-group2
 *
 */
public class Talk extends JTextPane{

	private StyledDocument doc;
	private Style s;

	/**
	 * Constructs a new Talk.
	 * Sets the layout preferences.
	 * 
	 */
	public Talk(){

		doc = this.getStyledDocument();
		this.addStylesToDocument();
		this.setEditable(false);
		this.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createRaisedBevelBorder(), 
				BorderFactory.createLineBorder(Color.LIGHT_GRAY, 5)));
		this.setEditable(false);		
	}


	/**
	 * Add specific styles to document (talk).
	 * Font Family definition : SansSerif.
	 * Italic, Bold styles.
	 * Font Size definitions: Small = 10, regular = 16.
	 * 
	 */
	protected void addStylesToDocument() {
		//Initialize some styles.
		Style def = StyleContext.getDefaultStyleContext().
		getStyle(StyleContext.DEFAULT_STYLE);

		Style regular = doc.addStyle("regular", def);
		StyleConstants.setFontFamily(def, "SansSerif");

		s = doc.addStyle("italic", regular);
		StyleConstants.setItalic(s, true);

		s = doc.addStyle("bold", regular);
		StyleConstants.setBold(s, true);

		s = doc.addStyle("small", regular);
		StyleConstants.setFontSize(s, 10);

		s = doc.addStyle("large", regular);
		StyleConstants.setFontSize(s, 16);
		
		
        
	}

	/**
	 * Add a new message (mood) to the talk.
	 * 
	 * @param date Creation date of mood
	 * @param nickname NickName of the user who created the mood
	 * @param mood Mood Image icon
	 */
	public void addMoodMessage(Date date, String nickname, String mood){

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		try {
			doc.insertString(doc.getLength(), "\n\n" + "        " + dateFormat.format(date), doc.getStyle("small"));
			doc.insertString(doc.getLength(), "  " + nickname, doc.getStyle("bold"));
			doc.insertString(doc.getLength(), " is feeling ", doc.getStyle("regular"));
			
			s = doc.addStyle("icon", null);
			StyleConstants.setIcon(s, new ImageIcon(mood + ".png"));
			doc.insertString(doc.getLength(), "icon", doc.getStyle("icon"));
			
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}


	public void addMessage(String message) {
		try {
			doc.insertString(doc.getLength(), "  " + message, doc.getStyle("bold"));
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}

}
